package com.minelx.datamicroscope.querylanguage.common;

import java.util.List;
import java.util.Objects;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.IntStream.concat;
import static java.util.stream.IntStream.range;

public class ListUtil {
    public static <E> Stream<List<E>> split(List<E> source, E item) {
        IntStream indexOfItems = range(0, source.size())
                .filter(index -> Objects.equals(source.get(index), item));
        int[] indexes = concat(concat(IntStream.of(-1), indexOfItems), IntStream.of(source.size()))
                .toArray();
        return range(0, indexes.length - 1)
                .mapToObj(at -> source.subList(indexes[at] + 1, indexes[at + 1]));
    }
}
